package RabbitUI;


import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;


import Debog.Debog;
import JSON.JSONArray;
import JSON.JSONException;
import JSON.JSONObject;

public class RabbitUIConf extends Debog{

	String configfile;
	JSONObject json ;

	public RabbitUIConf(String string,Hashtable<Integer,Rectangle> dim){
		configfile=string;

		FileReader fr;
		try {
			fr = new FileReader(configfile);
			BufferedReader b = new BufferedReader(fr);
			json=new JSONObject(b.readLine());
			fr.close();
		} catch (FileNotFoundException e) {
			json=new JSONObject();
			try {
				json.put("rectangles", JSONizeRectangles(dim));
			} catch (JSONException e1) {
				e1.printStackTrace();
			}                        
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public String[] getFilesToOpen(){
		String[] files=null; 
		JSONArray jarray;
		try {
			jarray = json.optJSONArray("files");
			if (jarray!=null){
				files=new String[jarray.length()];
				for (int i=0;i<jarray.length();i++){
					files[i]=jarray.getString(i);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return files;
	}
	public void save(String[] files_to_open,Hashtable<Integer,Rectangle> dimensions,
			String lnf, Hashtable<Integer,Boolean> outils_visibles){
		try {
			FileWriter fw=new FileWriter(configfile);

			json=new JSONObject();
			if (files_to_open!=null) json.put("files", files_to_open);
			json.put("rectangles", JSONizeRectangles(dimensions));
			if (lnf!=null) json.put("lookandfeel",lnf);
			if (outils_visibles!=null) json.put("visibles", JSONizeVisibles(outils_visibles));
			debog("Saving configuration to "+configfile);
			debog(json.toString(1));
			json.write(fw);
			fw.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	public void save_more(String key,Object value){
		try {
			FileReader fr = new FileReader(configfile);
			BufferedReader b = new BufferedReader(fr);
			json=new JSONObject(b.readLine());
			fr.close();
			
			FileWriter fw=new FileWriter(configfile);
			if (value==null){debog("save_more : NULL");}
			json.put(key, value);
			//System.out.println(json.toString(1));
			json.write(fw);
			fw.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	public Object get_more(String key){
		return json.opt(key);
	}
	private JSONArray JSONizeRectangle(Rectangle rect) throws JSONException{
		int[] tab=new int[4];
		tab[0]=rect.x;
		tab[1]=rect.y;
		tab[2]=rect.width;
		tab[3]=rect.height;
		return new JSONArray(tab);
	}
	private JSONArray JSONizeVisibles(Hashtable<Integer,Boolean> visibles) throws JSONException{
		boolean[] tab=new boolean[visibles.size()];
		for (int i=0;i<visibles.size();i++){
			tab[i]= visibles.get(i);
		}
		return new JSONArray(tab);
	}
	private JSONArray JSONizeRectangles(Hashtable<Integer,Rectangle> rect) throws JSONException{
		JSONArray[] tab=new JSONArray[rect.size()];
		for (int i=0;i<rect.size();i++){
			tab[i]=JSONizeRectangle(rect.get(i));
		}
		return new JSONArray(tab);
	}
	private Rectangle unJSONizeRectangle(JSONArray rect) throws JSONException{
		return new Rectangle(rect.getInt(0),rect.getInt(1),rect.getInt(2),rect.getInt(3));
	}
	public Rectangle[] getDimensions() {
		Rectangle[] rectangles=null; 
		JSONArray jarray;
		try {
			jarray = json.getJSONArray("rectangles");
			rectangles=new Rectangle[jarray.length()];
			for (int i=0;i<jarray.length();i++){
				rectangles[i]=unJSONizeRectangle(jarray.getJSONArray(i));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return rectangles;
	}
	public String getLookAndFeel() {
		return (String) json.opt("lookandfeel");
	}

	public boolean[] getOutilsVisibles() {
		boolean[] viso=null;
		JSONArray jarray;
		try {
			jarray = json.optJSONArray("visibles");
			if (jarray!=null){
				viso=new boolean[jarray.length()];
				for (int i=0;i<jarray.length();i++){
					viso[i]=jarray.getBoolean(i);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();	
		}
		return viso;
	}

}